package com.lsh.oms.core.service.finBill;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.enums.TaskStatus;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.model.order.OrderRoHead;
import com.lsh.oms.core.model.order.OrderSignHead;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.service.order.OrderQueryService;
import com.lsh.oms.core.service.other.RoOrderService;
import com.lsh.oms.core.service.other.SignOrderService;
import com.lsh.oms.core.service.po.OmsTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/5/17
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
@Slf4j
public class FinBillService {

    @Autowired
    private SignOrderService signOrderService;

    @Autowired
    private RoOrderService roOrderService;

    @Autowired
    private OmsTaskService omsTaskService;

    @Autowired
    private OrderQueryService orderQueryService;

    @Transactional
    public boolean addSignTask(Integer startTime, Integer endTime, TaskType taskType) {

        List<OrderSignHead> orderSignHeads = signOrderService.selectByTimes(startTime, endTime);


        if (taskType.equals(TaskType.Bill_SIGN_BY_MQ)) {
            orderSignHeads.addAll(signOrderService.findInvalidReceiptsBetween(startTime, endTime));
        }

        if (orderSignHeads == null || orderSignHeads.isEmpty()) {
            return true;
        }
        log.info("mq sign push task find task size: {}", orderSignHeads.size());
        for (OrderSignHead orderSignHead : orderSignHeads) {
            boolean dealFlag = false;
            omsTaskService.add(this.builderOmsTask(taskType, String.valueOf(orderSignHead.getReceiptOrderId()), dealFlag, orderSignHead.getVenderId()));
            log.info("mq sign push task add task: {}", orderSignHead.getReceiptOrderId());
        }

        return true;
    }

    @Transactional
    public boolean addSignTask(List<OrderSignHead> orderSignHeads) {

        if (orderSignHeads == null || orderSignHeads.isEmpty()) {
            return true;
        }
        log.info("mq sign push task find task size: {}", orderSignHeads.size());
        for (OrderSignHead orderSignHead : orderSignHeads) {

            OrderHead orderHead = orderQueryService.findByCode(orderSignHead.getOrderId(), false, false);

            boolean dealFlag = false;
            if (orderHead != null) {
                JSONObject extJson = JSON.parseObject(orderHead.getExt());
                String orderDc = extJson.getString("orderDc");
                if ("LSx01".equals(orderDc)) {
                    dealFlag = true;
                }
            }

            omsTaskService.add(this.builderOmsTask(TaskType.Bill_SIGN, String.valueOf(orderSignHead.getReceiptOrderId()), dealFlag, orderHead.getVenderId()));
        }

        return true;
    }

    @Transactional
    public List<OrderSignHead> selectSignHeads(Integer startTime, Integer endTime) {

        List<OrderSignHead> orderSignHeads = signOrderService.selectByTimes(startTime, endTime);

        return orderSignHeads;
    }

    @Transactional
    public List<Long> selectRoIds(Integer startTime, Integer endTime) {

        List<Long> returnOrderAllIds = roOrderService.selectByTimes(startTime, endTime);

        if (returnOrderAllIds == null || returnOrderAllIds.isEmpty()) {
            return Collections.emptyList();
        }

        List<Long> returnOrderSubIds = roOrderService.selectByParams(returnOrderAllIds);

        if (returnOrderSubIds == null || returnOrderSubIds.isEmpty()) {
            return Collections.emptyList();
        }
//        log.info("【处理反仓单task】 插入数据 size {}", returnOrderSubIds.size());

        return returnOrderSubIds;
    }


    @Transactional
    public boolean addRoTask(Integer startTime, Integer endTime, TaskType taskType) {
        List<Long> returnOrderIds;

        if (mqPushTask(taskType)) {
            returnOrderIds = roOrderService.findBetween(startTime, endTime);
        } else {
            List<Long> returnOrderAllIds = roOrderService.selectByTimes(startTime, endTime);

            if (returnOrderAllIds == null || returnOrderAllIds.isEmpty()) {
                return true;
            }

            returnOrderIds = roOrderService.selectByParams(returnOrderAllIds);
        }
        if (returnOrderIds == null || returnOrderIds.isEmpty()) {
            return true;
        }
        log.info("【day处理反仓单task】 插入数据 size {}", returnOrderIds.size());
        for (Long returnOrderId : returnOrderIds) {

            OrderRoHead roHead = roOrderService.getHeadByReturnOrderId(returnOrderId);
            this.omsTaskService.add(this.builderOmsTask(taskType, String.valueOf(returnOrderId), false, roHead.getVenderId()));
            log.info("mq ro push task add task: {}", returnOrderId);
        }

        return true;
    }

    private boolean mqPushTask(TaskType taskType) {
        return taskType.equals(TaskType.Bill_RO_BY_MQ);
    }

    @Transactional
    public boolean addRoTask(List<Long> returnOrderSubIds) {

        if (returnOrderSubIds == null || returnOrderSubIds.isEmpty()) {
            return true;
        }

        log.info("mq ro push task find task size: {}", returnOrderSubIds.size());
        for (Long returnOrderId : returnOrderSubIds) {
            OrderRoHead roHead = roOrderService.getHeadByReturnOrderId(returnOrderId);

            omsTaskService.add(this.builderOmsTask(TaskType.Bill_RO, String.valueOf(returnOrderId), false, roHead.getVenderId()));
        }

        return true;
    }


    private OmsTask builderOmsTask(TaskType taskType, String refCode, boolean dealFlag, Long venderId) {

        int transTime = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());

        OmsTask omsTask = new OmsTask();
        omsTask.setExecCount(0);
        omsTask.setTaskType(taskType.getCode());
        omsTask.setCreateTime(transTime);
        omsTask.setUpdateTime(transTime);
        omsTask.setRefCode(refCode);
        omsTask.setVenderId(venderId);

        if (dealFlag) {
            omsTask.setTaskStatus(TaskStatus.TASK_DEAL_SUCCESS.getCode());
        } else {
            omsTask.setTaskStatus(TaskStatus.TASK_UN_DEAL.getCode());
        }

        return omsTask;
    }

}
